package com.saifan.wyy_ov.ui.onlishop;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.BaseActivity;
import com.saifan.wyy_ov.data.bean.EvaluationBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.ui.album.ui.PhotoAlbumActivity;

import com.saifan.wyy_ov.utils.DialogUtil;
import com.saifan.wyy_ov.utils.PictureUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

/**
 * @author WangLi
 * @date 2015年3月20日 下午8:12:00
 * @Description 提交评论页面
 * @version V1.0
 */

public class SubmitCommentsActivity extends BaseActivity {
	private ArrayList<String> selectList;
	private EditText ed_context;
	private GridView pic_grid;
	private CheckBox cb_isAnonymous;
	private CommentsPicAdapter adapter;
	private IGetData getData;
	private Toolbar toolbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit_comments);
		selectList = new ArrayList<String>();
		getData = new GetData();
		bindViews();
	}

	private void bindViews() {
		 setEnabledNavigation(true);
		ed_context = (EditText) findViewById(R.id.ed_context);
		pic_grid = (GridView) findViewById(R.id.pic_grid);
		cb_isAnonymous = (CheckBox) findViewById(R.id.cb_isAnonymous);
		adapter = new CommentsPicAdapter(selectList, SubmitCommentsActivity.this);
		pic_grid.setAdapter(adapter);

	}

	public void submit(View view) {
		if (StringUtil.isNullOrEmpty(ed_context.getText().toString())) {
			ToastUtil.showToast(SubmitCommentsActivity.this, "请输入您的评论内容");
			return;
		}
		RequestParams params = new RequestParams();
		EvaluationBean bean = new EvaluationBean();
		bean.setContent(ed_context.getText().toString());
		bean.setUserID(String.valueOf(getIntent().getIntExtra("StoreID", -1)));
		bean.setEvaluationPeopleID(getRoomInfoBean().getWYKHDA_ZJ());
		if (cb_isAnonymous.isChecked()) {
			bean.setEvaluationPeople("匿名");
		} else {
			bean.setEvaluationPeople(getRoomInfoBean().getWYKHDA_LC());
		}
		params.addBodyParameter("Evaluation", (new Gson()).toJson(bean));
		List<String> path = new ArrayList<String>();
		for (int i = 0; i < selectList.size(); i++) {
			path.add(PictureUtil.getCompressBitmap(selectList.get(i)));
		}
		for (int i = 0; i < path.size(); i++) {
			params.addBodyParameter("img" + i, new File(path.get(i)));
		}
		getData.getData(Api.Comment.EvaluationSubmit, params, new RequestCallBack<String>() {

			@Override
			public void onStart() {
				super.onStart();
				DialogUtil.showProgressDialog(SubmitCommentsActivity.this, "正在请求...");
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				DialogUtil.closeProgressDialog();
				if (responseInfo.result != null) {
					try {
						JSONObject jsonObject = new JSONObject(responseInfo.result.toString());
						String code = jsonObject.getString("Code");
						if (code.equals("200")) {
							SubmitCommentsActivity.this.finish();
							ToastUtil.showToast(SubmitCommentsActivity.this, "提交成功");
						} else {
							ToastUtil.showToast(SubmitCommentsActivity.this, "提交失败");
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				DialogUtil.closeProgressDialog();
			}
		});
	}

	public void selectImg(View view) {
		Intent intent = new Intent(SubmitCommentsActivity.this, PhotoAlbumActivity.class);
		intent.putStringArrayListExtra("data", selectList);
		startActivityForResult(intent,1);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode==RESULT_OK){
			if (requestCode==1){
				if (data!=null){
					selectList = data.getStringArrayListExtra("data");
					adapter.list = selectList;
					adapter.notifyDataSetChanged();
				}


			}
		}
	}
}
